package com.wyp168.leetcode;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

/**
 * @ClassName StockSpannerSolution
 * @Title TODO leetcode 901. 股票价格跨度
 * @Description TODO 维护一个StockSpanner类，返回股票每天的最大跨度（以当前天的价格往回遍历，找到连续的小于等于当前天数价格的股票价格，并统计其数量）
 * @Author wyp168
 * @Date 2022/10/21 08:40
 */
public class StockSpannerSolution {
    public static void main(String[] args) {
        StockSpannerSolution solution = new StockSpannerSolution();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            System.out.println(solution.next(scanner.nextInt()));
        }
    }

    Deque<Integer> stack;
    Deque<Integer> weight;

    //TODO
    public StockSpannerSolution() {
        stack = new ArrayDeque<>();
        weight = new ArrayDeque<>();
    }

    public int next(int price) {
        int w = 1;
        while (!stack.isEmpty() && stack.peek() <= price) {
            stack.pop();
            w += weight.pop();
        }
        stack.push(price);
        weight.push(w);
        return w;
    }
}
